<template>
  <div class="top-right-btn">
    <el-row>
      <!--<el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top" v-if="search">
        <el-button circle icon="Search" @click="toggleSearch()" />
      </el-tooltip>-->
      <el-tooltip class="item" effect="dark" content="刷新" placement="top">
        <el-button circle icon="Refresh" @click="refresh()" />
      </el-tooltip>
      <el-dropdown class="" trigger="click" placement="bottom-end" v-if="columns">
        <!--<el-tooltip class="item" effect="dark" content="显隐列" placement="top" >-->
          <el-button circle icon="Menu" @click="showColumn()" />
        <!--</el-tooltip>-->
        <template #dropdown>
          <el-dropdown-menu  >
            <el-row :gutter="20">
              <el-col :span="2">
                <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">列展示</el-checkbox>
              </el-col>
            </el-row>
            <hr/>
            <el-row :gutter="10" v-for="(item,index) in columnslist" :key="index">
              <el-col :span="2">
                <el-checkbox v-model="item.visible" @change="dataChange">{{item.label}}</el-checkbox>
              </el-col>
            </el-row>
          </el-dropdown-menu>
        </template>

      </el-dropdown>
    </el-row>

  </div>
</template>

<script setup lang="ts">
  const props = defineProps({
    columns: {
      type: Object,
      default: null,
    },
    search: {
      type: Boolean,
      default: true,
    },
    showSearch: {
      type: Boolean,
      default: false,
    },
  });

  const emits = defineEmits(['queryTable', "update:showSearch"]);

  // 显隐数据
  const value = ref([]);

  // 弹出层标题
  const title = ref("显示/隐藏");

  // 是否显示弹出层
  const open = ref(false);

  const isIndeterminate = ref(false);

  const columnslist = ref(props.columns);

  const checkAll = ref(true);

  // 刷新
  function refresh() {
    emits("queryTable");
  }

  // 搜索
  function toggleSearch() {
    emits("update:showSearch", !props.showSearch);
  }

  // 打开显隐列dialog
  function showColumn() {
    open.value = true;
  }

  // 显隐列初始默认隐藏列
  for (let item in props.columns) {
    if (props.columns[item].visible === false) {
      value.value.push(parseInt(item));
    }
  }

  // 右侧列表元素变化
  function dataChange(data) {
    let count = 0;
    let keyCount = 0;
    for (let item in columnslist.value){
      if(columnslist.value[item].visible){
        count++;
      }
      keyCount++;
    }
    isIndeterminate.value = count > 0 && count < keyCount;
    if(count == 0){
      checkAll.value = false
    }
    else if(count == keyCount){
      checkAll.value = true;
    }
  }

  //全选
  function handleCheckAllChange(e){
    isIndeterminate.value = false;
    for (let item in columnslist.value){
      columnslist.value[item].visible = e;
    }
  }

</script>

<style lang='scss' scoped>
  .top-right-btn{
    margin-left: 10px;
  }
  :deep(.el-transfer__button) {
    border-radius: 50%;
    padding: 12px;
    display: block;
    margin-left: 0px;
  }
  :deep(.el-transfer__button:first-child) {
    margin-bottom: 10px;
  }
  :deep(.el-checkbox){
    height: auto;
  }
  .el-dropdown{
    margin-left: 15px;
  }
  .el-dropdown-menu{
    width: 120px;
    padding: 10px 20px;
  }
  .el-checkbox--medium{
    height: 26px;
  }
</style>
